package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.GridLayout;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;

import entities.Person;
import fachlogik.Administration;

import java.awt.GridBagLayout;
import java.awt.TextField;
import java.util.Date;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

public class MainWindow {

private JFrame         frmLogin;
private JTextField     tf_user;
private Administration instance;
private Person         p;

private MainWindow     mainw;
private JPasswordField tf_password;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frmLogin.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainWindow() {
    initialize();
    instance = Administration.getInstance();
    p = new Person();
    mainw = this;
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException
            | IllegalAccessException | UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmLogin = new JFrame();
    frmLogin.setTitle("Login");
    frmLogin.setBounds(100, 100, 320, 255);
    frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmLogin.setResizable(false);
    frmLogin.setLocationRelativeTo(null);

    JMenuBar menuBar = new JMenuBar();
    frmLogin.setJMenuBar(menuBar);

    JMenu mnMenu = new JMenu("Menu");
    menuBar.add(mnMenu);

    JMenuItem mntmClose = new JMenuItem("Close");
    mntmClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    mnMenu.add(mntmClose);
    frmLogin.getContentPane().setLayout(null);

    tf_user = new JTextField();
    tf_user.setBounds(130, 40, 114, 19);
    frmLogin.getContentPane().add(tf_user);
    tf_user.setColumns(10);

    tf_password = new JPasswordField();
    tf_password.setBounds(130, 78, 114, 19);
    frmLogin.getContentPane().add(tf_password);

    JLabel lblNewLabel = new JLabel("User: ");
    lblNewLabel.setBounds(29, 42, 70, 15);
    frmLogin.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Password: ");
    lblNewLabel_1.setBounds(29, 80, 80, 15);
    frmLogin.getContentPane().add(lblNewLabel_1);

    JButton btnNewButton = new JButton("OK");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            p.setUsername(tf_user.getText());
            p.setPassword(tf_password.getText());
            if (instance.login(p)) {
                // JOptionPane.showMessageDialog(null, "Login erfolgreich");
                // p = instance.getUser();
                CalendarWindow cw = new CalendarWindow(mainw);
                frmLogin.setVisible(false);
            }
            else
                JOptionPane.showMessageDialog(null, "Wrong Username and/or Password!");
        }
    });
    btnNewButton.setBounds(29, 140, 117, 25);
    frmLogin.getContentPane().add(btnNewButton);

    JButton btn_cancel = new JButton("Cancel");
    btn_cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    btn_cancel.setBounds(158, 140, 117, 25);
    frmLogin.getContentPane().add(btn_cancel);

    JButton btnNewUser = new JButton("Add New User");
    btnNewUser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (!tf_user.getText().equals("")) {
                if (!tf_password.getText().equals("")) {
                    if (!instance.userExists(tf_user.getText())) {
                    	p = new Person();
                        p.setUsername(tf_user.getText());
                        p.setPassword(tf_password.getText());
                        instance.addPerson(p);
                        instance.login(p);
                        CalendarWindow cw = new CalendarWindow(mainw);
                        JOptionPane.showMessageDialog(null, "Benutzer wurde angelegt!",
                                "Information", 1);
                        frmLogin.setVisible(false);
                    }
                    else
                        JOptionPane.showMessageDialog(null,
                                "Username alreads exists, please choose another one!", "Error!", 0);
                }
                else
                    JOptionPane.showMessageDialog(null, "Enter a Password!", "Error!", 0);
            }
            else
                JOptionPane.showMessageDialog(null, "Enter a Username!", "Error!", 0);
        }
    });
    btnNewUser.setBounds(29, 177, 246, 25);
    frmLogin.getContentPane().add(btnNewUser);
}

public JPasswordField getTf_password() {
    return tf_password;
}

public void setTf_password(JPasswordField tf_password) {
    this.tf_password = tf_password;
}

public JFrame getFrmLogin() {
    return frmLogin;
}

public Administration getInstance() {
    return this.instance;
}
}
